var post_jsonData = parent.$("#post_jsonData").val();
var isNesting = parent.$("#isNesting").val();
var json_post_jsonData = eval("("+post_jsonData+")");
var startAlgorithmIds = parent.$("#startAlgorithmIds").val();
var endAlgorithmIds = parent.$("#endAlgorithmIds").val();
var index = parent.layer.getFrameIndex(window.name);
var datas = parent.$("#datas").val();
var json_datas = eval("("+datas+")");
var isSave = false;
window.onload = function(){
    if(isNesting=="0"){
        $(".radio-position").show();
    }
    $.ajax({
        url:constUrl+"ws/model",
        type:'post',
        dataType:'json',
        data:{
            "operateType":"preview",
            "start":startAlgorithmIds,
            "end":endAlgorithmIds
        },
        success: function(responseText) {
            var jsonData = responseText;
            creatHtml(jsonData);
        },
        complete: function() {},
        error: function() {
            layer.alert("获取数据失败！")
        }
    });
    $('#saveModel').click(function () {
        if(!isSave){
            isSave = true;
            var name = $("#name").val();
            var InputText1 = $("#InputText1").val();
            var isReuse = $("input[name='again']:checked").val();
            $.ajax({
                url:constUrl+"ws/model",
                type:'post',
                data:{
                    "operateType":"addModel",
                    "data":JSON.stringify({
                        "name":name,
                        "isNesting":isNesting,//是否嵌套
                        "introduction":InputText1,
                        "isReuse":isReuse,//是否可复用
                        "json":json_post_jsonData   ,//模型的整体json
                        "nestJson":""//嵌套部分的json
                    })
                },
                dataType:'json',
                success: function(responseText) {
                    console.log(responseText);
                    if(responseText.result){
                        layer.confirm('保存成功，是否要创建新的任务？', {
                            btn: ['确认','取消'] //按钮
                        }, function(){
                            parent.window.location.href=constUrl+"/views/task/create.action?modelId="+responseText.modelId;
                        }, function(){
                            parent.layer.close(index);
                        });
                    }
                },
                complete: function() {},
                error: function() {
                    isSave = false;
                }
            })
        }
    });
    $('#closeIframe').click(function(){
        parent.layer.close(index);
    });
}
creatHtml = function(jsonData){
    var input_form = document.getElementById("input_form");
    var inputConfig = jsonData.start;
    input_form.innerHTML +=
        '<div class="form-title">' +
            '<h4>任务总体输入预览</h4>' +
        '</div>';
    for(var i=0; i<inputConfig.length; i++){
        for(var j=0; j<inputConfig[i].algorithmInputs.length; j++){
            input_form.innerHTML +=
                '<hr>' +
                '<div class="input">' +
                    '<div class="form-group input_name"> ' +
                        '<label class="col-sm-2">输入名称：</label> ' +
                        '<input type="text" class="form-control form-group output" readonly="readonly" value="'+inputConfig[i].name+"："+inputConfig[i].algorithmInputs[j].inputName+'"> ' +
                    '</div> ' +
                    '<div class="form-group"> ' +
                        '<label class="col-sm-2 control-label">简介</label> ' +
                        '<div class="col-sm-10"> ' +
                            '<p class="form-control-static">'+inputConfig[i].algorithmInputs[j].inputIntroduction+'</p> ' +
                        '</div> ' +
                    '</div> ' +
                '</div>';
        }
    }
    input_form.innerHTML += '<div style="clear:both"></div>';
    var output_form = document.getElementById("output_form");
    var outputConfig = jsonData.end;
    output_form.innerHTML +=
        '<div class="form-title">' +
            '<h4>任务总体输出预览</h4>' +
        '</div>';
    for(var i=0; i<outputConfig.length; i++){
        for(var j=0; j<outputConfig[i].algorithmOutputs.length; j++){
            output_form.innerHTML +=
                '<hr>' +
                '<div class="input">' +
                    '<div class="form-group input_name"> ' +
                        '<label class="col-sm-2">输出名称：</label> ' +
                        '<input type="text" class="form-control form-group output" readonly="readonly" value="'+outputConfig[i].name+"："+outputConfig[i].algorithmOutputs[j].outputName+'"> ' +
                    '</div> ' +
                    '<div class="form-group"> ' +
                        '<label class="col-sm-2 control-label">简介</label> ' +
                        '<div class="col-sm-10"> ' +
                            '<p class="form-control-static">'+outputConfig[i].algorithmOutputs[j].outputIntroduction+'</p> ' +
                        '</div> ' +
                    '</div> ' +
                '</div>';
        }
    }
    output_form.innerHTML += '<div style="clear:both"></div>';
    var data_form = document.getElementById("data_form");
    data_form.innerHTML +=
        '<div class="form-title">' +
            '<h4>外部数据源配置</h4>' +
        '</div>';
    if(json_datas.length!=0){
        for(var i=0; i<json_datas.length; i++){
            data_form.innerHTML +=
                '<hr>' +
                '<div class="data">' +
                    '<div class="form-group data_name"> ' +
                        '<label class="col-sm-2"><i class="param_name_text">'+json_datas[i].Name+'：'+json_datas[i].dataName+json_datas[i].dataNumber+'</i></label> ' +
                    '</div> ' +
                '</div> ';
        }
        data_form.innerHTML += '<div style="clear:both"></div>';
    }else {
        data_form.innerHTML += '<hr>' + '<div style="padding-left: 15px;">无</div>';
    }
}